﻿using System;
using Microsoft.Practices.ObjectBuilder;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMS.Model;
using System.Web.Script.Serialization;
using CMS.Logging;
using CMS.ViewModel;

namespace CMSWCSFSolution.CMS.Views
{
    public partial class CourseManage : BasePage, ICourseManageView
    {
        public static int RowCounts;
        private CourseManagePresenter _presenter;

        #region Protected Method
        /// <summary>
        /// Create New Presenter
        /// </summary>
        [CreateNew]
        public CourseManagePresenter Presenter
        {
            get
            {
                return this._presenter;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this._presenter = value;
                this._presenter.View = this;
            }
        }

        /// <summary>
        /// Page Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                PageSize = 5;
                if (!this.IsPostBack)
                {
                    SelectAction();
                }
            }
            catch (Exception ee)
            {
                Logging.LogWrite(ee.Message.ToString(), Logging.Category.Error, Logging.Priority.Highest);
                ShowMessage(3, "error");
            }
        }

        /// <summary>
        /// Select Actions Based on Client
        /// </summary>
        public void SelectAction()
        {
            try
            {
                string action = string.Empty;
                if (!string.IsNullOrEmpty(Request.Params["action"]))
                {
                    action = Request.Params["action"].ToString();
                }
                switch (action)
                {
                    case "init":
                        {
                            GetCourses();
                        };
                        break;
                    case "search":
                        {
                            GetCourses();
                        };
                        break;
                    case "delete":
                        {
                            if (_presenter.DeleteCourse())
                            {
                                GetCourses();
                            }
                            else
                            {
                                GetCourses();
                            }

                        };
                        break;
                    case "batch":
                        {
                            _presenter.ChangeCourseStatus();
                            GetCourses();
                        };
                        break;
                }
            }
            catch (Exception ee)
            {
                Logging.LogWrite(ee.Message.ToString(), Logging.Category.Error, Logging.Priority.High);
                ShowMessage(3, "error");
            }
        }

        /// <summary>
        /// Gets Courses and Change to Json Type
        /// </summary>
        public void GetCourses()
        {

            try
            {
                List<CoursePE> courseList = _presenter.GetCoursesListByCondition();
                string jsonString = new JavaScriptSerializer().Serialize(courseList).ToString();

                string returnData = string.Empty;
                returnData = "{";

                //total pages
                returnData += "\"totalpages\"";
                returnData += ":";
                returnData += "\"";
                returnData += PageCount;
                returnData += "\"";
                returnData += ",";

                //current page
                returnData += "\"page\"";
                returnData += ":";
                returnData += "\"";
                returnData += PageIndex;
                returnData += "\"";
                returnData += ",";


                //total records
                returnData += "\"totalrecords\"";
                returnData += ":";
                returnData += "\"";
                returnData += RowCounts;
                returnData += "\"";
                returnData += ",";

                //add json string
                returnData += "\"griddata\"";
                returnData += ":";
                returnData += jsonString;

                returnData += "}";

                Response.Write(returnData);
                Response.End();
            }
            catch (Exception ee)
            {
                Logging.LogWrite(ee.Message.ToString(), Logging.Category.Error, Logging.Priority.High);
                ShowMessage(3, "error");
            }

        }
        #endregion

        #region Public Property

        /// <summary>
        /// CourseName Textbox Value
        /// </summary>
        public string CourseName
        {
            get
            {
                if (string.IsNullOrEmpty(Request.Params["coursename"]))
                {
                    return string.Empty;
                }
                else
                {
                    return Request.Params["coursename"].ToString();
                }
            }
            set { }
        }

        /// <summary>
        /// CourseStatus From CheckBox
        /// </summary>
        public string CourseStatus
        {
            get
            {
                if (Request.Params["open"] == "true" && Request.Params["closed"] == "false")
                {
                    return "true";
                }
                else if (Request.Params["open"] == "false" && Request.Params["closed"] == "true")
                {
                    return "false";
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        /// <summary>
        /// CourseId From JqGrid
        /// </summary>
        public int CourseId
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.Params["courseId"]))
                {
                    return Convert.ToInt32(Request.Params["courseId"]);
                }
                else return 0;
            }
            set { }
        }

        /// <summary>
        /// CourseIdList from JqGrid
        /// </summary>
        public string[] CourseIdList
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.Params["courseidList"]))
                {
                    var courseIdList = Request.Params["courseidList"];
                    string[] stringArray = courseIdList.Split(new char[] { ',' });
                    return stringArray;
                }
                else
                    return null;
            }
            set { }
        }

        /// <summary>
        /// PageCount to Show All Pages
        /// </summary>
        public int PageCount
        {
            get
            {
                return (RowCount + PageSize - 1) / PageSize;
            }
        }

        /// <summary>
        /// RowCount to show all Records
        /// </summary>
        public int RowCount
        {
            get
            {
                return RowCounts;
            }
            set
            {
                RowCounts = value;
            }
        }

        /// <summary>
        /// PageSize for Paging Function
        /// </summary>
        public int PageSize
        {
            get;
            set;
        }

        /// <summary>
        /// PageIndex to Show Current Page
        /// </summary>
        public int PageIndex
        {
            get
            {
                return Convert.ToInt32(Request.Params["page"]);
            }
        }

        #endregion
    }
}

